//MINI UI解析
mini.parse();

//广告配置处理器类
function EditAdvCfgHandler() {
	this.data = {};
	this.type = mini.get("sacType");
	this.content = mini.get("sacContent");
	this.name = mini.get("sacName");
	this.order = mini.get("sacOrder");
	this.page = mini.get("sacPage");
	this.position = mini.get("sacPosition");
	this.url = mini.get("sacUrl");
	this.func = mini.get("sacFunction");
	this.state = mini.get("sacState");
	this.intro = mini.get("sacIntro");
	this.image = mini.get("image");
	this.remove = mini.get("remove");
	this.form = new mini.Form("editAdvCfgForm");
	
	this.wmc = new WebMgrConfig();
	
	this.image.limitType = this.wmc.advImgType;
	this.image.limitSize = this.wmc.advImgSize;
	this.image.flashUrl = __root + "/admin/resource/swfrpc/swfupload.swf";
	this.image.uploadUrl = __root + "/fileUploadContorller.do?uploadPath=" + __path;
	
	this.find = $("#find");
	this.find.attr({src : __root + "/admin/resource/css/ico/search.gif", onclick : "instance.search();"});
	
	this.viewer = $("#viewer");
	
	this.load = function(data) {
		data = mini.clone(data);
		this.data.sacId = data.sacId;
		
		if (!data.isedit) {
			this.readonly();
			mini.get("save").hide();
		}
		
		this.initialize();
		
		if (!data.sacId) {
			instance.remove.disable();
		} else {
			var param = { sacId : data.sacId };
			var json = mini.encode(param);
			$.ajax({
				url : __root + "/getAdvConfig.do", 
				type : "POST", 
				data : { data : json }, 
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.reload(result.message.sacType);
						instance.form.setData(result.message);
						if (result.message.sacImage) {
							instance.image.setText(result.message.sacImage);
							instance.review(result.message.sacImage, false);
						} else {
							instance.remove.disable();
						}
						
						if (!data.isedit) {
							instance.find.hide();
						}
					} else {
						instance.readonly();
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					instance.readonly();
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.save = function() {
		this.form.validate();
		if (this.form.isValid()) {
			var data = this.form.getData();
			data.sacId = this.data.sacId;
			data.sacImage = this.image.getText();
			
			var json = mini.encode(data);
			$.ajax({
				url : __root + "/saveAdvConfig.do", 
				type : "POST", 
				data: {
		        	data: json,
		        	lslUser: gbOperateUser,
		    		lslAddress: gbOperateIp
		        },
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.close("save");
					} else {
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.close = function(action) {
		if (CloseOwnerWindow) {
			CloseOwnerWindow(action);
		} else {
			window.close();
		}
	};
	
	this.search = function() {
		miniWin.afreshNewWin();
		miniWin.setParams(600, 350, "搜索内容");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("searchContent.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { type : instance.type.getValue(), request : "/getAdvContents.do" };
	        iframe.contentWindow.instance.load(data, instance.selected);
		});
		
		miniWin.show();
	};
	
	this.selected = function(content) {
		instance.content.setValue(content);
	};
	
	this.review = function(image, isdelete) {
		this.viewer.empty();
		
		if (!isdelete) {
			var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __path + "&newImgPath=" + image + "' " + 
				"frameborder='0' border='0' style='margin: 0px; padding: 0px; width: 100%; height: 100%;'/>";
			this.viewer.append(iframe);
		}
	};
	
	this.initialize = function() {
		var typeData = mini.clone(this.wmc.advType);
		typeData.removeAt(0);
		this.type.setData(typeData);
		
		var pageData = mini.clone(this.wmc.normalPage);
		pageData.removeAt(0);
		this.page.setData(pageData);
		
		var positionData = mini.clone(this.wmc.advPosition);
		positionData.removeAt(0);
		this.position.setData(positionData);
		
		var stateData = mini.clone(this.wmc.normalState);
		stateData.removeAt(0);
		this.state.setData(stateData);
	};
	
	this.reload = function(type) {
		this.content.enable();
		this.content.setValue(null);
		this.content.setUrl(__root + "/getAdvContent.do?type=" + type);
		
		if (type == this.wmc.advType[1].value) {
			this.content.disable();
			this.content.setValue("-");
			this.find.hide();
		} else if (type == this.wmc.advType[2].value) {
			this.find.hide();
		} else {
			this.find.show();
		}
	};
	
	this.readonly = function() {
		this.type.readOnly = true;
		this.content.readOnly = true;
		this.name.readOnly = true;
		this.order.readOnly = true;
		this.page.readOnly = true;
		this.position.readOnly = true;
		this.url.readOnly = true;
		this.func.readOnly = true;
		this.state.readOnly = true;
		this.intro.readOnly = true;
		this.image.flashUrl = null;
		this.image.disable();
		this.remove.disable();
	};
};

//广告配置处理器全局实例
var instance = new EditAdvCfgHandler();

//定义内容类型值改变事件处理程序
function onSacTypeValueChanged(e) {
	var type = instance.type.getValue();
	instance.reload(type);
}

//定义图片上传成功事件处理程序
function onImageUploadSuccess(e) {
	var data = mini.decode(e.serverData);
	
	instance.image.setText(data.imgPath);
	instance.review(data.imgPath, false);
	instance.remove.enable();
	
	mini.alert(e.file.name + " 上传成功！", "系统提示");
}

//定义图片上传失败事件处理程序
function onImageUploadError(e) {
	mini.alert(e.file.name + " 上传失败！", "系统提示");
}

//定义删除图片按钮点击事件处理程序
function onRemoveClick(e) {
	instance.image.setText(null);
	instance.review(null, true);
	instance.remove.disable();
}

//定义保存按钮点击事件处理程序
function onSaveClick(e) {
	instance.save();
}

//定义关闭按钮点击事件处理程序
function onCancelClick(e) {
	instance.close("cancel");
}
